package com.ity.teamanagement.result;

import com.ity.teamanagement.code.Code;
import com.ity.teamanagement.code.CommonCode;
import io.swagger.annotations.ApiModel;
import lombok.Data;

/**
 * @author coderFu
 * @create 2021/5/23
 */
@Data
@ApiModel("统一结果返回")
public class Result<T> {

    private boolean status;

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String message;

    /**
     * 返回内容
     */
    private T data;

    public Result(){}

    /**
     * 有返回值响应构造方法
     * @param code
     * @param data
     */
    public Result(Code code, T data){
        this.status = code.success();
        this.code = code.code();
        this.message = code.message();
        this.data = data;
    }

    /**
     * 无返回值响应构造方法
     * @param code
     */
    public Result(Code code){
        this.status = code.success();
        this.code = code.code();
        this.message = code.message();
        this.data = null;
    }

    /**
     * 以下构造方法方法直接使用
     */
    public static Result<Object> success(){
        return new Result<>(CommonCode.SUCCESS);
    }

    public static Result<Object> success(Code code){
        return new Result<>(code);
    }

    public static Result<Object> success(Object data){
        return new Result<>(CommonCode.SUCCESS,data);
    }

    public static Result<Object> update_success(Object data){
        return new Result<>(CommonCode.UPDATE_SUCCESS,data);
    }

    public static Result<Object> update_success(){
        return new Result<>(CommonCode.UPDATE_SUCCESS);
    }

    public static Result<Object> delete_success(Object data){
        return new Result<>(CommonCode.DELETE_SUCCESS,data);
    }

    public static Result<Object> delete_success(){
        return new Result<>(CommonCode.DATA_ERROR);
    }

    public static Result<Object> error(Code code){
        return new Result<>(code);
    }

    public static Result<Object> error(Code code, Object data){
        return new Result<>(code,data);
    }


}
